package cn.xm.lingshi.controller;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class YanZhengMaServlet extends HttpServlet {

	
	

		private static final long serialVersionUID = 1L;

		/**图片背景
		 * @return
		 */
		public Color GetRandomColor() {
			Random random = new Random();
			int r = random.nextInt(256);
			int g = random.nextInt(256);
			int b = random.nextInt(256);
			return new Color(r, g, b);

		}

		/**
		 * @param length
		 * @return
		 */
		public String GenerateCode(int length) {
			int rand;
			char code;
			String randomCode = "";
			Random random = new Random();
			for (int i = 0; i < length; i++) {
				rand = random.nextInt();
				if (rand % 3 == 0) {
					code = (char) ('A' + (char) rand % 26);
				} else {
					code = (char) ('0' + (char) rand % 10);
				}
				randomCode += String.valueOf(code);
			}
			return randomCode;
		}

		public BufferedImage Paint(String code, int width, int height) {

			int mapWidth = width == 0 ? (int) (code.length() * 16) : width;
			int mapHeight = height == 0 ? (int) 35 : height;

			BufferedImage map = new BufferedImage(mapWidth, mapHeight,
					BufferedImage.TYPE_INT_RGB);
			Graphics graph = map.getGraphics();

			graph.setColor(Color.WHITE);
			graph.fillRect(0, 0, mapWidth, mapHeight);
			// 设置字体样式
			graph.setFont(new Font(Font.SANS_SERIF, Font.PLAIN,
					(int) (height * 0.8)));

			// 添加噪点
			Random random = new Random();
			for (int i = 0; i < 50; i++) {
				int x = random.nextInt(map.getWidth());
				int y = random.nextInt(map.getHeight());
				int rgb = GetRandomColor().getRGB();
				map.setRGB(x, y, rgb);
			}

			// 定义每个验证码字符颜色
			Color[] c = { Color.BLACK, Color.RED, Color.BLUE, Color.GREEN,
					Color.ORANGE, Color.BLACK, Color.DARK_GRAY, Color.LIGHT_GRAY };

			int fx = 0;
			for (int i = 0; i < code.length(); i++) {
				int x = random.nextInt(7);
				graph.setColor(c[x]);
				graph.drawString(code.charAt(i) + "", fx, (int) (height * 0.8));
				fx += (width / code.length()) * (Math.random() * 0.3 + 0.8);
			}
			graph.dispose();
			return map;
		}

		public void doGet(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {
			response.setContentType("image/jpeg");
			response.setDateHeader("expries", -1);
			response.setHeader("Cache-Control", "no-cache");
			response.setHeader("Pragma", "no-cache");
			//获得 4位验证码
			String code=GenerateCode(4);
			//用与储存验证码   到session验证是否正确
			
			request.getSession().setAttribute("yzm", code);
			
			
			ImageIO.write(Paint(code, 85, 35), "jpg",response.getOutputStream());

			
		}

		public void doPost(HttpServletRequest request, HttpServletResponse response)
				throws ServletException, IOException {

			this.doGet(request, response);
		}
	}
